//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.Text;
using System.Resources;
using System.IO;
using System.Drawing;
using System.Globalization;
using System.Collections;
using EntLibContrib.Resource.Managers;

#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
#else
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using ClassInitialize = NUnit.Framework.TestFixtureSetUpAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
#endif

namespace EntLibContrib.Resource.Tests
{
	[TestClass]
	public class ResourceDataNodeFixture
	{
		private static string pathName;
		private static string baseName;
		private static string extension;
		private static string fileName;
		private static string typeName;
		private static string resourceName;
		private static string resourceTypeName;
		private static object resourceValue;

		[ClassInitialize]
#if NUNIT
		public void Initialize()
#else
		public static void Initialize(TestContext context)
#endif
		{
			pathName = "path";
			baseName = "base";
			extension = ".resx";
			fileName = @"path\file.bmp";
			typeName = "System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
			resourceName = "ResourceName";
			resourceTypeName = resourceName.GetType().AssemblyQualifiedName;
			resourceValue = "ResourceValue";
		}

		[TestMethod]
		public void BuildResourceFileName()
		{
			string resourceFileName = @"path\base.en.resx";
			Assert.AreEqual(resourceFileName, ResourceFileName.Build(pathName, baseName, new CultureInfo("en"), extension));
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void BuildResourceFileNameWithNullPathName()
		{
			ResourceFileName.Build(null, baseName, new CultureInfo("en"), extension);
			Assert.Fail();
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void BuildResourceFileNameWithNullBaseName()
		{
			ResourceFileName.Build(pathName, null, new CultureInfo("en"), extension);
			Assert.Fail();
		}

		[TestMethod]
		public void BuildResourceFileNameWithNullCultue()
		{
			string resourceFileName = @"path\base.resx";
			Assert.AreEqual(resourceFileName, ResourceFileName.Build(pathName, baseName, null, extension));
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void BuildResourceFileNameWithNullExtension()
		{
			ResourceFileName.Build(pathName, baseName, new CultureInfo("en"), null);
			Assert.Fail();
		}

		[TestMethod]
		public void CreateResourceFileRef()
		{
			ResourceFileRef resourceFileRef = new ResourceFileRef(fileName, typeName);
			Assert.IsNotNull(resourceFileRef);
			Assert.AreEqual(fileName, resourceFileRef.FileName);
			Assert.AreEqual(typeName, resourceFileRef.TypeName);
			Assert.IsNull(resourceFileRef.TextFileEncoding);
		}

		[TestMethod]
		public void CreateResourceFileRefWithEncoding()
		{
			ResourceFileRef resourceFileRef = new ResourceFileRef(fileName, typeName, new UnicodeEncoding());
			Assert.IsNotNull(resourceFileRef);
			Assert.AreEqual(fileName, resourceFileRef.FileName);
			Assert.AreEqual(typeName, resourceFileRef.TypeName);
#if NUNIT
			Assert.IsInstanceOfType(typeof(UnicodeEncoding), resourceFileRef.TextFileEncoding);
#else
			Assert.IsInstanceOfType(resourceFileRef.TextFileEncoding, typeof(UnicodeEncoding));
#endif
		}

		[TestMethod]
		public void CreateResourceDataNode()
		{
			ResourceDataNode resourceDataNode = new ResourceDataNode(resourceName, resourceValue);
			Assert.IsNotNull(resourceDataNode);
			Assert.AreEqual(resourceName, resourceDataNode.Name);
			Assert.AreEqual(resourceTypeName, resourceDataNode.TypeName);
			Assert.AreEqual(resourceValue, resourceDataNode.Value);
			Assert.IsNull(resourceDataNode.Comment);
			Assert.IsNull(resourceDataNode.FileRef);
		}

		[TestMethod]
		public void CreateResourceDataNodeWithComment()
		{
			string comment = "Test Comment";
			ResourceDataNode resourceDataNode = new ResourceDataNode(resourceName, resourceValue);
			resourceDataNode.Comment = comment;
			Assert.IsNotNull(resourceDataNode);
			Assert.AreEqual(resourceName, resourceDataNode.Name);
			Assert.AreEqual(resourceTypeName, resourceDataNode.TypeName);
			Assert.AreEqual(resourceValue, resourceDataNode.Value);
			Assert.AreEqual(comment, resourceDataNode.Comment);
			Assert.IsNull(resourceDataNode.FileRef);
		}

		[TestMethod]
		public void CreateResourceDataNodeWithFileRef()
		{
			string typeName = typeof(ResourceFileRef).AssemblyQualifiedName;
			ResourceFileRef resourceFileRef = new ResourceFileRef(fileName, typeName);
			ResourceDataNode resourceDataNode = new ResourceDataNode(resourceName, resourceFileRef);
			Assert.AreEqual(resourceName, resourceDataNode.Name);
			Assert.AreEqual(typeName, resourceDataNode.TypeName);
			Assert.IsNull(resourceDataNode.Value);
			Assert.IsNotNull(resourceDataNode.FileRef);
			Assert.AreEqual(fileName, resourceDataNode.FileRef.FileName);
			Assert.AreEqual(typeName, resourceDataNode.FileRef.TypeName);
		}
	}
}
